package com.comp3111.happymeal;

import com.configuration.Configuration;
import com.configuration.Owner;
import com.controller.UserUpdate;
import com.user.Contact;
import com.user.DatabaseHandler;
import com.user.LoginRegister;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class UserProfileEditUserProfileActivity extends NavigationDrawerActivity {

	ImageView iv_edit_profile_picture;
	EditText et_edit_profile_name;
	EditText et_edit_profile_description;
	Button btn_edit_profile_next;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);
        createDrawer();
        
        //Link the variable to the variable
        iv_edit_profile_picture = (ImageView) findViewById(R.id.edit_profile_picture);
        et_edit_profile_name = (EditText) findViewById(R.id.edit_profile_name);
        et_edit_profile_description = (EditText) findViewById(R.id.edit_profile_description);
        btn_edit_profile_next = (Button) findViewById(R.id.edit_profile_next);
        
        //Set the text based on the server data
      	et_edit_profile_name.setText(Owner.name);
      	et_edit_profile_description.setText(Owner.description);
      	if(Owner.profile_picture != ""){
      		iv_edit_profile_picture.setTag(Configuration.baseURL + Owner.profile_picture);
      		new DownloadImagesForImageView().execute(iv_edit_profile_picture);
      	}
      	
      	iv_edit_profile_picture.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
			}
		});
      		
      				
      	btn_edit_profile_next.setOnClickListener(new OnClickListener(){
      		public void onClick(View v){
      			
      			// Store data to Owner Class static variables
      			// TODO : Store correct value into the Class static variables
      			Owner.name = et_edit_profile_name.getText().toString();
      			Owner.description = et_edit_profile_description.getText().toString();
      			//Owner.profile_picture = user.getProfilePicture();
      			
      			
      			//Upload entered data to server
      			UserUpdate userUpdate = new UserUpdate();
      			final int FAIL = 0;
      			if(FAIL == userUpdate.updateUserByTelephone(Owner.telephone,et_edit_profile_name.getText().toString(), et_edit_profile_description.getText().toString())){
      				Toast.makeText(getApplicationContext(), "Unable to connect to server, please try again", Toast.LENGTH_LONG).show();
      				return;
      			}
      			
      			//Jump to DiscoverPageActivity
      			Intent intentPass = new Intent(UserProfileEditUserProfileActivity.this, UserProfileActivity.class);
      			
      			//Pass pass intent								
      			intentPass.putExtra("USERID",Integer.toString(Owner.id));
      				
      			//Go to next page
      			startActivity(intentPass);
      			
      		}
      	});
    }
}